import { createRouter, createWebHistory } from 'vue-router';
// eslint-disable-next-line import/extensions
import MenuView from '../views/MenuView';
// eslint-disable-next-line import/extensions
import LoginView from '../views/LoginView.vue';
import CompanyView from '@/components/business/CompanyView';
import UserView from '@/components/system/UserView';
import TreeMenuView from '@/components/system/TreeMenuView';
import RoleView from '@/components/system/RoleView';
import NotFoundView from '@/components/NotFoundView';
import IndexView from '@/components/index/IndexView';
import { ElMessage } from 'element-plus';

// 懒加载
// () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue'),

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
  },
  {
    path: '/index',
    name: 'index',
    component: MenuView,
    meta: {
      breadcrumbKey: '首页'
    },
    children: [
      {
        path: '/index',
        name: 'index',
        component: IndexView,
        meta: {
          breadcrumbKey: '首页'
        },
      }
    ]
  },
  //系统管理
  {
    path: '/system',
    name: 'system',
    component: MenuView,
    meta: {
      breadcrumbKey: '系统管理'
    },
    children: [
      {
        path: '/system/menu',
        name: 'menu',
        component: TreeMenuView,
        meta: {
          breadcrumbKey: '菜单管理'
        }
      },
      {
        path: '/system/user',
        name: 'user',
        component: UserView,
        meta: {
          breadcrumbKey: '用户管理'
        }
      },
      {
        path: '/system/role',
        name: 'role',
        component: RoleView,
        meta: {
          breadcrumbKey: '角色管理'
        }
      },
    ],
  },
  //业务管理
  {
    path: '/business',
    name: 'business',
    component: MenuView,
    meta: {
      breadcrumbKey: '业务管理'
    },
    children: [
      {
        path: '/business/company',
        name: 'company',
        component: CompanyView,
        meta: {
          breadcrumbKey: '公司管理'
        }
      },
      {
        path: '/business/department',
        name: 'department',
        component: CompanyView,
        meta: {
          breadcrumbKey: '部门管理'
        }
      },
    ],
  },
  //404
  {
    path: '/:pathMatch(.*)',
    redirect: '/404'
  },
  {
    path: '/404',
    name: 'NotFound',
    component: NotFoundView
  }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

//全局前置路由守卫判断登录状态 ==> 由axios响应拦截器拦截数据信息进行判断是否需要权限
router.beforeEach((to, from, next) => {
  console.log(to.path);
  if (to.path !== '/login') {
    let user = localStorage.getItem('user');
    let token = localStorage.getItem('token');
    console.log(user, token);
    if (user === '' || user === null ||
      token === '' || token === null) {
      next('/login');
      ElMessage({
        message: '请先登录',
        type: 'error',
        grouping: true,
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
